import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/register',
    component: () => import('@/views/register')
  },
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/',
    component: () => import('@/views/menu'),
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem('token')) {
        next()
      } else {
        alert('请先登录')
        next({ path: '/login' })
      }
    },
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/components/home')
      },
      {
        path: 'changeUserMsg',
        component: () => import('@/components/usermsg')
      },
      {
        path: 'changeUserPic',
        component: () => import('@/components/changeUserPic')
      },
      {
        path: 'changeUserPassword',
        component: () => import('@/components/changeUserPassword')
      },
      {
        path: 'articleClassification',
        component: () => import('@/components/classification')
      },
      {
        path: 'articleList',
        component: () => import('@/components/articleList')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
export default router
